<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2019/9/5
 * Time: 10:41
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class City extends Base {
    public function index(){
        if(request()->isAjax ()){
            extract(input());
            if(isset($user_name) && $user_name != ''){
                $where['district'] = ['like', '%' . $user_name . '%'];
            }
            $where['level'] =2;
            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('area')->where($where)->count();// 获取总条数
            $lists = Db::name('area')
                ->where($where)
                ->page($Nowpage,$limits)
                ->order('sort desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    /**
     * 排序编辑
     * @return mixed|\think\response\Json
     */
    public function updateSort(){
        $id = input('id');
        $param = [
            'sort'=>input('value'),
        ];
        $res = Db::name('area')->where('district_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }

    /**
     * 删除
     * @return \think\response\Json
     */
    public function del(){
        $id = input('id');
        $res = Db::table('up_area')->where('district_id', $id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 上架
     * @return \think\response\Json
     */
    public function bannerOn(){
        $banner_id = input('id');
        $res = Db::table('up_area')->where('district_id', $banner_id)->setField('hot', 1);
        if($res){
            return json(['code' => 200, 'msg' => '上架成功']);
        }else{
            return json(['code' => 200, 'msg' => '上架失败']);
        }
    }

    /**
     * 下架
     * @return \think\response\Json
     */
    public function bannerDown(){
        $banner_id = input('id');
        $res = Db::table('up_area')->where('district_id', $banner_id)->setField('hot', 2);
        if($res){
            return json(['code' => 200, 'msg' => '下架成功']);
        }else{
            return json(['code' => 200, 'msg' => '下架失败']);
        }
    }
}